import unittest
from time import sleep

from selenium.common import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait

from HTMLTestReportCN import DirAndFiles
from base.base_driver import driver_setup
from base.base_page import BasePage
from po.camera.camera_page import CameraPage
from po.common.common import Common
from po.home.home_page import HomePage
from po.tools.tools_page import ToolsPage

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class TestToolsPdf(unittest.TestCase, BasePage):

    @classmethod
    def setUpClass(cls):
        cls.driver = driver_setup()
        cls.home_page = HomePage(driver=cls.driver)
        cls.camera_page = CameraPage(driver=cls.driver)
        cls.common = Common(driver=cls.driver)
        cls.tools_page = ToolsPage(driver=cls.driver)

    def test_01_pdf2word(self):
        """PDF转Word-过滤-横屏查看-复制"""
        self.home_page.btn_tools()
        self.drag(0.5, 0.5, 0.4, 0.1, 800)
        self.tools_page.btn_pdf2word()
        self.camera_page.func_import_document()
        self.common.btn_filter()
        self.common.btn_all_select()
        sleep(1)
        DirAndFiles.get_screenshot(self.driver, "PDF转Word")
        sleep(1)
        self.common.btn_land()
        WebDriverWait(self.driver, 200).until(EC.presence_of_element_located((By.ID, self.HW_ENV + "picnum")))
        sleep(1)
        DirAndFiles.get_screenshot(self.driver, "PDF转Word_横屏查看")
        sleep(1)
        self.driver.keyevent(4)
        self.common.btn_copy()
        sleep(1)

    def test_02_pdf2word_export(self):
        """PDF转Word-导出"""
        self.common.btn_export()
        self.common.func_export()
        sleep(1)
        self.common.btn_back()
        sleep(1)

    def test_03_pdf2excel(self):
        """PDF转Excel-横屏查看-复制"""
        self.tools_page.btn_pdf2excel()
        self.camera_page.func_import_document()
        DirAndFiles.get_screenshot(self.driver, "PDF转Excel")
        self.common.btn_land()
        WebDriverWait(self.driver, 200).until(EC.presence_of_element_located((By.ID, self.HW_ENV + "picnum")))
        sleep(1)
        DirAndFiles.get_screenshot(self.driver, "PDF转Excel_横屏查看")
        sleep(1)
        self.driver.keyevent(4)
        self.common.btn_copy()
        sleep(1)

    def test_04_pdf2excel_export(self):
        """PDF转Excel-导出"""
        self.common.btn_export()
        self.common.func_export()
        sleep(1)
        self.common.btn_back()
        sleep(1)

    def test_05_pdf2img(self):
        """PDF转图片"""
        self.tools_page.btn_pdf2img()
        self.camera_page.func_import_document()
        DirAndFiles.get_screenshot(self.driver, "PDF转图片")
        sleep(1)
        self.common.btn_back()
        sleep(1)

    def test_06_pdfMerge(self):
        """PDF合并"""
        self.drag(0.5, 0.5, 0.4, 0.1, 800)
        self.tools_page.btn_pdfMerge()
        pdfs = self.by_uiautomators('new UiSelector().resourceId("' + self.HW_ENV + 'item_checkbox")')
        for pdf in pdfs:
            pdf.click()
        self.camera_page.btn_import()
        # 取消加密pdf的导入
        while True:
            try:
                btn_cancel = self.by_id((self.HW_ENV + 'cancel'))
                btn_cancel.click()
            except NoSuchElementException:
                break

        WebDriverWait(self.driver, 50).until(EC.presence_of_element_located((By.ID, self.HW_ENV + "img_share")))
        DirAndFiles.get_screenshot(self.driver, "PDF合并")
        sleep(1)
        self.common.btn_back()
        sleep(1)

    def test_07_pdf2img_long(self):
        """PDF转长图-导出"""
        self.tools_page.btn_pdf2img_long()
        self.camera_page.func_import_document()
        DirAndFiles.get_screenshot(self.driver, "PDF转长图")
        self.tools_page.btn_img_share()
        self.common.func_export()
        sleep(1)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
